#include "playwidget.h"
#include "ui_playwidget.h"

#include <QPainter>
#include <QDebug>

PlayWidget::PlayWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlayWidget)
{
    ui->setupUi(this);

    mDecodeThread = new DecodeVideoThread;
    connect(mDecodeThread,SIGNAL(sig_send_image(QImage)),this,SLOT(slot_receive_image(QImage)));
    connect(mDecodeThread,&DecodeVideoThread::finished,mDecodeThread,&DecodeVideoThread::deleteLater);
}

PlayWidget::~PlayWidget()
{
    if(mDecodeThread->isRunning()){
        stop();
    }
    delete ui;
}

void PlayWidget::set_url(QString url)
{
    mDecodeThread->set_url(url);
}

void PlayWidget::play()
{
    stop();
    mDecodeThread->start();
}

void PlayWidget::stop()
{
    qDebug()<<__FILE__<<__FUNCTION__;
    if(mDecodeThread->isRunning()){
        //线程里是自定义的死循环，通过quit()和exit()函数停止不了。
//        mDecodeThread->requestInterruption();
//        mDecodeThread->quit();
//        mDecodeThread->wait(100);

        //自定义的运行停止函数
        mDecodeThread->stop_run();
    }
    mImg.fill(Qt::black);
}

void PlayWidget::slot_receive_image(const QImage &rImg)
{
//    qDebug()<<__FILE__<<__FUNCTION__;
    mImg = rImg.scaled(this->size());
    update();
}

void PlayWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawImage(0,0,mImg);
}
